import requests
import os
import hashlib
import hmac
cookies = {
    '_qimei_uuid42': '19419002905100ed08fa627a0318e15c638b646415',
    '_qimei_i_3': '70e053d6c70c528d9493f9650fd572b5f7baa5f8135b01d6b0877c5e23c72239376561943c89e2b48489',
    '_qimei_fingerprint': '1c5e15937c13894b8df3125c265ff775',
    'hy_source': 'web',
    'hy_user': 'c903525ab38242efa670fc443640557b',
    'hy_token': 'amlqbRV7Feb+gXpvj/ixQsAX1rY/82hWXh1L0v4fBL7bIxLhKVeqRJfmAGL5248MUfWrls3/ZQj8NJyeCPA43Q==',
    '_qimei_h38': 'e147df7308fa627a0318e15c0200000fc1941b',
    '_qimei_i_1': '66bf7ad7945e578f9790fd3053d222e6f5e9f5f1115a0bd7b58a7b582493206c616361c63980e6dd849df4d5',
}

headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    # Already added when you pass json=
    # 'content-type': 'application/json',
    'origin': 'https://yuanbao.tencent.com',
    'priority': 'u=1, i',
    'referer': 'https://yuanbao.tencent.com/chat/naQivTmsDa/6339f68f-b845-492d-9f70-2f106fb07ed4',
    'sec-ch-ua': '"Microsoft Edge";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    't-userid': 'c903525ab38242efa670fc443640557b',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0',
    'x-agentid': 'naQivTmsDa/6339f68f-b845-492d-9f70-2f106fb07ed4',
    'x-commit-tag': 'd241137f',
    'x-hy92': 'e147df7308fa627a0318e15c0200000fc1941b',
    'x-hy93': '19419002905100ed08fa627a0318e15c638b646415',
    'x-id': 'c903525ab38242efa670fc443640557b',
    'x-instance-id': '5',
    'x-language': 'zh-CN',
    'x-os_version': 'Windows(10)-Blink',
    'x-platform': 'win',
    'x-requested-with': 'XMLHttpRequest',
    'x-source': 'web',
    # Requests sorts cookies= alphabetically
    # 'cookie': '_qimei_uuid42=19419002905100ed08fa627a0318e15c638b646415; _qimei_i_3=70e053d6c70c528d9493f9650fd572b5f7baa5f8135b01d6b0877c5e23c72239376561943c89e2b48489; _qimei_fingerprint=1c5e15937c13894b8df3125c265ff775; hy_source=web; hy_user=c903525ab38242efa670fc443640557b; hy_token=amlqbRV7Feb+gXpvj/ixQsAX1rY/82hWXh1L0v4fBL7bIxLhKVeqRJfmAGL5248MUfWrls3/ZQj8NJyeCPA43Q==; _qimei_h38=e147df7308fa627a0318e15c0200000fc1941b; _qimei_i_1=66bf7ad7945e578f9790fd3053d222e6f5e9f5f1115a0bd7b58a7b582493206c616361c63980e6dd849df4d5',
}
# 获取文件的size
def obj2str(file_path, response):
    file_size = os.path.getsize(file_path)
    Host = response['bucketName'] + "." + response['accelerateDomain']
    return f"content-length={file_size}&host={Host}"

def sha1_to_string(x):
    """
    接收输入 x，将其进行 SHA-1 哈希处理并转换为十六进制字符串
    :param x: 输入数据，可以是字符串或字节类型
    :return: SHA-1 哈希处理后的十六进制字符串
    """
    if isinstance(x, str):
        # 如果 x 是字符串，将其编码为字节类型
        x = x.encode('utf-8')
    elif not isinstance(x, bytes):
        # 如果 x 既不是字符串也不是字节类型，尝试将其转换为字符串再编码
        x = str(x).encode('utf-8')
    # 创建 SHA-1 哈希对象
    sha1 = hashlib.sha1()
    # 更新哈希对象的数据
    sha1.update(x)
    # 获取十六进制字符串表示的哈希结果
    return sha1.hexdigest()
def hmac_sha1_to_string(o,y):
    """
    接收密钥 y 和消息 o，进行 HMAC-SHA1 计算并转换为十六进制字符串
    :param y: 密钥，字节类型或可编码为字节类型的数据
    :param o: 消息，字节类型或可编码为字节类型的数据
    :return: HMAC-SHA1 计算后的十六进制字符串
    """
    if isinstance(y, str):
        y = y.encode('utf-8')
    if isinstance(o, str):
        o = o.encode('utf-8')
    # 创建 HMAC 对象，使用 SHA1 算法
    h = hmac.new(y, o, hashlib.sha1)
    # 获取十六进制字符串表示的结果
    return h.hexdigest()


file_path = 'test.html'  # 替换为实际文件路径

json_data = {
    'fileName': file_path,
    'docFrom': 'localDoc',
    'docOpenId': '',
}

UserInfo = requests.post('https://yuanbao.tencent.com/api/resource/genUploadInfo', cookies=cookies, headers=headers, json=json_data).json()

















# S, b
b = hmac_sha1_to_string(f"{UserInfo['startTime']};{UserInfo['expiredTime']}", UserInfo['encryptTmpSecretKey'])
s = "\n".join(["sha1", f"{UserInfo['startTime']};{UserInfo['expiredTime']}",sha1_to_string( "\n".join( ['put', UserInfo['location'], '',obj2str(file_path, UserInfo) , ""])) , ""])
signature = hmac_sha1_to_string(s,b)

headers = {
    'Accept': '*/*',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'Authorization': f'q-sign-algorithm=sha1&q-ak={UserInfo['encryptTmpSecretId']}&q-sign-time={UserInfo['startTime']};{UserInfo['expiredTime']}&q-key-time={UserInfo['startTime']};{UserInfo['expiredTime']}&q-header-list=content-length;host&q-url-param-list=&q-signature={signature}',
    'Cache-Control': '',
    'Connection': 'keep-alive',
    'Content-Length': str(os.path.getsize(file_path)),
    'Content-Type': 'application/octet-stream',
    'Origin': 'https://yuanbao.tencent.com',
    'Referer': 'https://yuanbao.tencent.com/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'cross-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0',
    'sec-ch-ua': '"Microsoft Edge";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'x-cos-security-token': UserInfo['encryptToken'],
}
# 请求url
url = f"https://{UserInfo['bucketName']}.{UserInfo['accelerateDomain']}{UserInfo['location']}"
response = requests.put(url, headers=headers, data=open(file_path, 'rb').read())

print(url)

print(UserInfo)

print(response.status_code)
